home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / midi / midifile.lha / Midifile / mf1to0.c < prev    next >
C/C++ Source or Header  |  1995-08-13  |  14KB  |  553 lines

  1. /*
  2.  * f1to0.c 4/30/89
  3.  *
  4.  * Author:  Michael S. Czeiszperger
  5.  * Purpose: The program converts format 1 MIDI files to format 0
  6.  *          MIDI files.  It is meant to demonstrate the use of
  7.  *          the midifile library.  The internal data format is
  8.  *          a linked list of MIDI and SMF meta events.
  9.  *
  10.  *          The spec is available from:
  11.  *               International MIDI Association
  12.  *               5316 West 57th Street
  13.  *               Los Angeles, CA 90056
  14.  *
  15.  *          An in-depth description of the spec can also be found
  16.  *          in the article "Introducing Standard MIDI Files", published
  17.  *          in Electronic Musician magazine, April, 1988.
  18.  * 
  19.  * usage: f1to0 infile outfile
  20.  * 
  21.  */
  22.  
  23. char *_version = "$VER: mf1to0 V 1.00 (24-Jan-95)";
  24.  
  25. #include <stdio.h>
  26. #include <ctype.h>
  27.  
  28. #include <stdlib.h>
  29.  
  30. #include "midifile.h"
  31.  
  32. enum PACKET_TYPE
  33.   {
  34.     MIDI_EVENT, META_EVENT
  35.   };
  36.  
  37. struct MIDIpacket        /* Stucture for MIDI events */
  38.   {
  39.     enum PACKET_TYPE p_type;    /* What type of packet is it?               */
  40.     unsigned long time;        /* absolute time of event, in ms increments */
  41.     unsigned int m_type;    /* What kind of MIDI message?               */
  42.     unsigned int chan;        /* The MIDI channel                         */
  43.     unsigned long len;        /* length of the midi message               */
  44.     unsigned char *data;    /* pointer to the data                      */
  45.     struct MIDIpacket *next, *before;    /* point to next packet */
  46.   }
  47.  *sequence_start, *sequence_end;
  48.  
  49. /* 
  50.    This example structure is not very portable. It does, however,
  51.    mirror the logical operation of the header, and on some machines can
  52.    be written and read directly.  It assumes that shorts are at least
  53.    two bytes and chars are at least one byte.
  54.  */
  55. struct header_type
  56.   {
  57.     short format;
  58.     short ntrks;
  59.     union
  60.       {
  61.     short quarter_note;
  62.     struct
  63.       {
  64.         char format;
  65.         char resolution;
  66.       }
  67.     smpte;
  68.       }
  69.     division;
  70.   }
  71. header;
  72.  
  73. /* These lines are needed to use the library */
  74. FILE *fpIn, *fpOut;
  75. extern long Mf_currtime;
  76. mygetc ()
  77. {
  78.   return (getc (fpIn));
  79. }
  80. int 
  81. myputc (char c)
  82. {
  83.   return (putc (c, fpOut));
  84. }
  85.  
  86. main (argc, argv)
  87.      int argc;
  88.      char **argv;
  89. {
  90.   if (argc != 3)
  91.     {
  92.       printf ("Usage: mf1to0 <infile> <outfile>\n");
  93.       printf ("mf1to0 converts a MIDI format 1 file to MIDI format 0\n");
  94.       printf ("Original code by Michael S. Czeiszperger\n");
  95.       printf ("Amiga port by Andreas Jung (ajung@cs.uni-sb.de)\n");
  96.       printf ("Recompiled using the Dice Professional compiler\n");
  97.  
  98.       exit (0);
  99.     }
  100.  
  101.   if ((fpIn = fopen (argv[1], "r")) == 0L)
  102.     {
  103.       printf ("mf1to0: unable to open file '%s' for reading.\n", argv[1]);
  104.       exit (0);
  105.     }
  106.  
  107.   if ((fpOut = fopen (argv[2], "w")) == 0L)
  108.     {
  109.       printf ("mf1to0: unable to open file '%s' for writing.\n", argv[2]);
  110.       exit (0);
  111.     }
  112.  
  113.   init_funcs ();
  114.  
  115.   /* read the midi file */
  116.   mfread ();
  117.  
  118.   /* write a single track */
  119.   mfwrite ((int) header.format, 1, (int) header.division.quarter_note, fpOut);
  120. }
  121.  
  122. myheader (format, ntrks, division)
  123.      int format, ntrks, division;
  124. {
  125.   header.format = format;
  126.   header.ntrks = ntrks;
  127.   header.division.quarter_note = division;
  128. }
  129.  
  130. myerror (s)
  131.      char *s;
  132. {
  133.   printf ("%10ld: %s\n", Mf_currtime, s);
  134. }
  135.  
  136. /*
  137.  * mywritetrack()
  138.  *
  139.  * Sample showing how to use the library routines to write out a track.
  140.  * Returns 1 if successful, and -1 if not.
  141.  *
  142.  */
  143. int 
  144. mywritetrack (int track)
  145. {
  146.   struct MIDIpacket *current;
  147.   unsigned long delta_time;
  148.  
  149.   current = sequence_start;    /* init to point at the beginning of the list */
  150.  
  151.   /* shuffle through each element in the linked list, writing out
  152.      the data in the standard MIDI format */
  153.   while (current != 0L)
  154.     {
  155.       if (current == sequence_start)
  156.     delta_time = current->time;
  157.       else
  158.     delta_time = current->time - (current->before)->time;
  159. #ifdef DEBUG
  160.       printf ("ticks = %lu event = %d packet = %d chan = %d len = %lu\n",
  161.           current->time, current->m_type, current->p_type, current->chan, current->len);
  162. #endif
  163.  
  164.       if (current->p_type == MIDI_EVENT)
  165.     {
  166.       if (mf_write_midi_event (delta_time, current->m_type, current->chan, current->data, current->len) < 0)
  167.         return (-1);
  168.     }
  169.       else if (current->p_type == META_EVENT)
  170.     {
  171.       if (mf_write_meta_event (delta_time, current->m_type, current->data, current->len) < 0)
  172.         return (-1);
  173.     }
  174.       else
  175.     fprintf (stderr, "Unknown MIDI packet encounted.\n");
  176.  
  177.       current = current->next;
  178.     }
  179.   return (1);
  180. }                /* end of write_track() */
  181.  
  182.  
  183. /*
  184.  * This routine adds a MIDI or META event into the linked list.  
  185.  */
  186. void 
  187. add_packet (ticks, event_type, packet_type, chan, data, len)
  188.      unsigned long ticks;    /* absolute ticks of event */
  189.      int event_type;        /* note_on, note_off, etc  */
  190.      enum PACKET_TYPE packet_type;    /* What kind of data?      */
  191.      unsigned char *data;    /* already allocated data  */
  192.      unsigned long len;        /* length of data          */
  193. {
  194.   struct MIDIpacket *scratch, *insert_pt, *get_insert_point ();
  195.  
  196. #ifdef DEBUG
  197.   printf ("ticks = %d event = %d packet = %d chan = %d len = %d\n", ticks, event_type, packet_type, chan, len);
  198. #endif
  199.   scratch = (struct MIDIpacket *) malloc (sizeof (struct MIDIpacket));
  200.  
  201.   if (scratch == NULL)
  202.     {
  203.       fprintf (stderr, "Sorry, out of memory!\n");
  204.       exit (1);
  205.     }
  206.  
  207.   scratch->data = data;
  208.   scratch->m_type = event_type;
  209.   scratch->p_type = packet_type;
  210.   scratch->chan = chan;
  211.   scratch->len = len;
  212.   scratch->time = ticks;
  213.  
  214.   /* Where in the linked list should this packet go? */
  215.   insert_pt = get_insert_point (sequence_start, sequence_end, ticks);
  216.  
  217.   if (sequence_start == 0L)    /* the list is empty */
  218.     {
  219.       sequence_start = sequence_end = scratch;
  220.       scratch->before = 0L;
  221.       scratch->next = 0L;
  222.     }
  223.   else if (insert_pt == 0L)    /* insert the new bottom record */
  224.     {
  225.       sequence_end->next = scratch;
  226.       scratch->before = sequence_end;
  227.       scratch->next = 0L;
  228.       sequence_end = scratch;
  229.     }
  230.   else if (insert_pt == sequence_start)        /* insert the new top record */
  231.     {
  232.       sequence_start->before = scratch;
  233.       scratch->before = 0L;
  234.       scratch->next = sequence_start;
  235.       sequence_start = scratch;
  236.     }
  237.   else
  238.     /* insert before the insert pointer */
  239.     {
  240.       scratch->next = insert_pt;
  241.       scratch->before = insert_pt->before;
  242.       insert_pt->before = scratch;
  243.       (scratch->before)->next = scratch;
  244.     }
  245. }                /* end of add_packet() */
  246.  
  247. /*
  248.  * get_insert_point()
  249.  *    A routine to help insertion into a linked list,  
  250.  *    by returning a pointer to the position in the list
  251.  *    where the MIDI packet should be inserted based on
  252.  *    the time the MIDI event occurs. This could be a lot
  253.  *    faster, but was left simple for easy debugging.
  254.  *    If you'd like to speed this up, please go ahead!
  255.  */
  256. struct MIDIpacket *
  257. get_insert_point (top, bottom, time)
  258.      struct MIDIpacket *top, *bottom;
  259.      unsigned long time;
  260. {
  261.   struct MIDIpacket *index;
  262.   /* this keeps track of the last inset point, making it easier */
  263.   /* to find the next one assuming that most times will be     */
  264.   /* consecutive.                                               */
  265.   static struct MIDIpacket *insert_pointer;
  266.  
  267.   if (top == 0L || bottom == 0L)
  268.     return (0L);
  269.   if (time > bottom->time)
  270.     return (0L);        /* tack onto end of list */
  271.   if (time < top->time)
  272.     return (top);        /* insert as the new first record */
  273.  
  274.   /* check around where the last packet was inserted */
  275.   if (insert_pointer != 0L)
  276.     index = insert_pointer;
  277.   else
  278.     {
  279.       index = sequence_start;
  280.       insert_pointer = index;
  281.     }
  282.   if (time > index->time)
  283.     {
  284.       while (time > index->time && index != 0L)
  285.     index = index->next;
  286.       return (index);
  287.     }
  288.   else if (time < index->time)
  289.     {
  290.       while (time < index->time && index != 0L)
  291.     index = index->before;
  292.       return (index->next);
  293.     }
  294.   else
  295.     return (insert_pointer);
  296. }                /* end of get_insert_point() */
  297.  
  298. /* Routines called from midifile lib */
  299. mynoteon (chan, c1, c2)
  300.      int chan, c1, c2;
  301. {
  302.   unsigned char *data;
  303.  
  304.   data = (unsigned char *) malloc (sizeof (char) * 2);
  305.   if (data == NULL)
  306.     {
  307.       fprintf (stderr, "Sorry, out of memory!\n");
  308.       exit (1);
  309.     }
  310.   data[0] = (unsigned char) c1;
  311.   data[1] = (unsigned char) c2;
  312.   add_packet ((unsigned long) Mf_currtime, note_on, MIDI_EVENT, chan, data, (unsigned long) 2);
  313. }
  314.  
  315. mynoteoff (chan, c1, c2)
  316.      int chan, c1, c2;
  317. {
  318.   unsigned char *data;
  319.  
  320.   data = (unsigned char *) malloc (sizeof (char) * 2);
  321.   if (data == NULL)
  322.     {
  323.       fprintf (stderr, "Sorry, out of memory!\n");
  324.       exit (1);
  325.     }
  326.   data[0] = (unsigned char) c1;
  327.   data[1] = (unsigned char) c2;
  328.   add_packet ((unsigned long) Mf_currtime, note_off, MIDI_EVENT, chan, data, (unsigned long) 2);
  329. }
  330.  
  331. mypressure (chan, pitch, pressure)
  332.      int chan, pitch, pressure;
  333. {
  334.   unsigned char *data;
  335.  
  336.   data = (unsigned char *) malloc (sizeof (char) * 2);
  337.   if (data == NULL)
  338.     {
  339.       fprintf (stderr, "Sorry, out of memory!\n");
  340.       exit (1);
  341.     }
  342.   data[0] = (unsigned char) pitch;
  343.   data[1] = (unsigned char) pressure;
  344.   add_packet ((unsigned long) Mf_currtime, poly_aftertouch, MIDI_EVENT, chan, data, (unsigned long) 2);
  345. }
  346.  
  347. int 
  348. mykeypressure (int chan, int pitch, int pressure)
  349. {
  350.   unsigned char *data;
  351.  
  352.   data = (unsigned char *) malloc (sizeof (char) * 2);
  353.   if (data == NULL)
  354.     {
  355.       fprintf (stderr, "Sorry, out of memory!\n");
  356.       exit (1);
  357.     }
  358.   data[0] = (unsigned char) pitch;
  359.   data[1] = (unsigned char) pressure;
  360.   add_packet ((unsigned long) Mf_currtime, channel_aftertouch, MIDI_EVENT, chan, data, (unsigned long) 2);
  361. }
  362.  
  363. myparameter (chan, control, value)
  364.      int chan, control, value;
  365. {
  366.   unsigned char *data;
  367.  
  368.   data = (unsigned char *) malloc (sizeof (char) * 2);
  369.   if (data == NULL)
  370.     {
  371.       fprintf (stderr, "Sorry, out of memory!\n");
  372.       exit (1);
  373.     }
  374.   data[0] = (unsigned char) control;
  375.   data[1] = (unsigned char) value;
  376.   add_packet ((unsigned long) Mf_currtime, control_change, MIDI_EVENT, chan, data, (unsigned long) 2);
  377. }
  378.  
  379. mytempo (long microsecs)
  380. {
  381.   unsigned char *data;
  382.   data = (unsigned char *) malloc (sizeof (char) * 3);
  383.   if (data == NULL)
  384.     {
  385.       fprintf (stderr, "Sorry, out of memory!\n");
  386.       exit (1);
  387.     }
  388.   data[2] = (unsigned) (microsecs & 0xff);
  389.   data[1] = (unsigned) ((microsecs >> 8) & 0xff);
  390.   data[0] = (unsigned) ((microsecs >> 16) & 0xff);
  391.   add_packet ((unsigned long) Mf_currtime, set_tempo, META_EVENT, 0, data, (unsigned long) 3);
  392. }
  393.  
  394. int 
  395. myvarlen (int type, int len, char *msg)
  396. {
  397.   unsigned char *data;
  398.   int i;
  399.  
  400.   data = (unsigned char *) malloc ((unsigned) (sizeof (char) * len));
  401.   if (data == NULL)
  402.     {
  403.       fprintf (stderr, "Sorry, out of memory!\n");
  404.       exit (1);
  405.     }
  406.   for (i = 0; i < len; i++)
  407.     data[i] = msg[i];
  408.   add_packet ((unsigned long) Mf_currtime, type, META_EVENT, 0, data, (unsigned long) len);
  409. }
  410.  
  411. mysmpte (int hour, int min, int sec, int frame, int fract)
  412. {
  413.   unsigned char *data;
  414.  
  415.   data = (unsigned char *) malloc (sizeof (char) * 5);
  416.   if (data == NULL)
  417.     {
  418.       fprintf (stderr, "Sorry, out of memory!\n");
  419.       exit (1);
  420.     }
  421.   data[0] = hour;
  422.   data[1] = min;
  423.   data[2] = sec;
  424.   data[3] = frame;
  425.   data[4] = fract;
  426.   add_packet ((unsigned long) Mf_currtime, smpte_offset, META_EVENT, 0, data, (unsigned long) 5);
  427. }
  428.  
  429. myprogram (chan, program)
  430.      int chan, program;
  431. {
  432.   unsigned char *data;
  433.   data = (unsigned char *) malloc (sizeof (char) * 1);
  434.   if (data == NULL)
  435.     {
  436.       fprintf (stderr, "Sorry, out of memory!\n");
  437.       exit (1);
  438.     }
  439.   data[0] = (unsigned char) program;
  440.   add_packet ((unsigned long) Mf_currtime, program_chng, MIDI_EVENT, chan, data, (unsigned long) 1);
  441. }
  442.  
  443. mypitchbend (chan, msb, lsb)
  444.      int chan, msb, lsb;
  445. {
  446.   unsigned char *data;
  447.   data = (unsigned char *) malloc (sizeof (char) * 2);
  448.   if (data == NULL)
  449.     {
  450.       fprintf (stderr, "Sorry, out of memory!\n");
  451.       exit (1);
  452.     }
  453.   data[0] = (unsigned char) msb;
  454.   data[1] = (unsigned char) lsb;
  455.   add_packet ((unsigned long) Mf_currtime, pitch_wheel, MIDI_EVENT, chan, data, (unsigned long) 2);
  456. }
  457.  
  458. mysysex (len, msg)
  459.      int len;
  460.      char *msg;
  461. {
  462.   unsigned char *data;
  463.   int i;
  464.  
  465.   data = (unsigned char *) malloc ((unsigned) (sizeof (char) * len));
  466.   if (data == NULL)
  467.     {
  468.       fprintf (stderr, "Sorry, out of memory!\n");
  469.       exit (1);
  470.     }
  471.   for (i = 0; i < len; i++)
  472.     data[i] = msg[i];
  473.   add_packet ((unsigned long) Mf_currtime, system_exclusive, MIDI_EVENT, 0, data, (unsigned long) len);
  474. }
  475.  
  476. myseqnum (num)
  477.      int num;
  478. {
  479.   unsigned char *data;
  480.   data = (unsigned char *) malloc (sizeof (char) * 1);
  481.   if (data == NULL)
  482.     {
  483.       fprintf (stderr, "Sorry, out of memory!\n");
  484.       exit (1);
  485.     }
  486.   data[0] = (unsigned char) num;
  487.   add_packet ((unsigned long) Mf_currtime, sequence_number, META_EVENT, 0, data, (unsigned long) 1);
  488. }
  489.  
  490. mytimesig (int numer, int denom, int clocks, int qnotes)
  491. {
  492.   unsigned char *data;
  493.  
  494.   data = (unsigned char *) malloc (sizeof (char) * 4);
  495.   if (data == NULL)
  496.     {
  497.       fprintf (stderr, "Sorry, out of memory!\n");
  498.       exit (1);
  499.     }
  500.   data[0] = numer;
  501.   data[1] = denom;
  502.   data[2] = clocks;
  503.   data[3] = qnotes;
  504.   add_packet ((unsigned long) Mf_currtime, time_signature, META_EVENT, 0, data, (unsigned long) 4);
  505. }
  506.  
  507. mykeysig (sharpflat, minor)
  508.      int sharpflat, minor;
  509. {
  510.   unsigned char *data;
  511.  
  512.   data = (unsigned char *) malloc (sizeof (char) * 2);
  513.   if (data == NULL)
  514.     {
  515.       fprintf (stderr, "Sorry, out of memory!\n");
  516.       exit (1);
  517.     }
  518.   data[0] = sharpflat;
  519.   data[1] = minor;
  520.   add_packet ((unsigned long) Mf_currtime, key_signature, META_EVENT, 0, data, (unsigned long) 2);
  521. }
  522.  
  523. init_funcs ()
  524. {
  525.   sequence_start = 0L;
  526.   sequence_end = 0L;
  527.  
  528.   /* needed for reading */
  529.   Mf_getc = mygetc;
  530.   Mf_error = myerror;
  531.   Mf_header = myheader;
  532.   Mf_noteon = mynoteon;
  533.   Mf_noteoff = mynoteoff;
  534.   Mf_pressure = mypressure;
  535.   Mf_parameter = myparameter;
  536.   Mf_pitchbend = mypitchbend;
  537.   Mf_program = myprogram;
  538.   Mf_chanpressure = mykeypressure;
  539.   Mf_sysex = mysysex;
  540.   Mf_metamisc = myvarlen;
  541.   Mf_seqspecific = myvarlen;
  542.   Mf_seqnum = myseqnum;
  543.   Mf_text = myvarlen;
  544.   Mf_timesig = mytimesig;
  545.   Mf_smpte = mysmpte;
  546.   Mf_tempo = mytempo;
  547.   Mf_keysig = mykeysig;
  548.  
  549.   /* needed for writing */
  550.   Mf_putc = myputc;
  551.   Mf_writetrack = mywritetrack;
  552. }
  553.